/*
 * Copyright (c) 2022 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import { TextEncoder } from './TextEncoder.js';
import { TextDecoder } from './TextDecoder.js';
import { ONEHUNDRED_KB_STRING, TEN_KB_BINARY } from './data.js';


// encoding
function Encode_utf8_Test(){
    const inputString = ONEHUNDRED_KB_STRING
    let encodedData;

    let startTime = Date.now();
    for (let i = 0; i < 10; i++) {
        encodedData = new TextEncoder('utf-8').encode(inputString);
    }
    let endTime = Date.now();

    if(encodedData == null){
        throw new Error('zxing-text-encodingDemo_encode_0100 Error')
    }
   print(`zxing-text-encoding_encode_utf8: ${endTime - startTime} ms`)
}

function Encode_utf16_Test(){
    const inputString = ONEHUNDRED_KB_STRING
    let encodedData;

    let startTime = Date.now();
    for (let i = 0; i < 10; i++) {
        encodedData = new TextEncoder('utf-16').encode(inputString);
    }
    let endTime = Date.now();

    if(encodedData == null){
        throw new Error('zxing-text-encodingDemo_encode_0100 Error')
    }
   print(`zxing-text-encoding_encode_utf16: ${endTime - startTime} ms`)
}

function Encode_csibm866_Test(){
    const inputString = ONEHUNDRED_KB_STRING
    let encodedData;

    let startTime = Date.now();
    for (let i = 0; i < 10; i++) {
        encodedData = new TextEncoder('csibm866').encode(inputString);
    }
    let endTime = Date.now();

    if(encodedData == null){
        throw new Error('zxing-text-encodingDemo_encode_0100 Error')
    }
   print(`zxing-text-encoding_encode_csibm866: ${endTime - startTime} ms`)
}

function Encode_gbk_Test(){
    const inputString = ONEHUNDRED_KB_STRING
    let encodedData;

    let startTime = Date.now();
    for (let i = 0; i < 10; i++) {
        encodedData = new TextEncoder('gbk').encode(inputString);
    }
    let endTime = Date.now();

    if(encodedData == null){
        throw new Error('zxing-text-encodingDemo_encode_0100 Error')
    }
   print(`zxing-text-encoding_encode_gbk: ${endTime - startTime} ms`)
}

function Encode_csbig5_Test(){
    const inputString = ONEHUNDRED_KB_STRING
    let encodedData;

    let startTime = Date.now();
    for (let i = 0; i < 10; i++) {
        encodedData = new TextEncoder('csbig5').encode(inputString);
    }
    let endTime = Date.now();

    if(encodedData == null){
        throw new Error('zxing-text-encodingDemo_encode_0100 Error')
    }
   print(`zxing-text-encoding_encode_csbig5: ${endTime - startTime} ms`)
}

function Encode_eucjp_Test(){
    const inputString = ONEHUNDRED_KB_STRING
    let encodedData;

    let startTime = Date.now();
    for (let i = 0; i < 10; i++) {
        encodedData = new TextEncoder('euc-jp').encode(inputString);
    }
    let endTime = Date.now();

    if(encodedData == null){
        throw new Error('zxing-text-encodingDemo_encode_0100 Error')
    }
   print(`zxing-text-encoding_encode_eucjp: ${endTime - startTime} ms`)
}

function Encode_cseuckr_Test(){
    const inputString = ONEHUNDRED_KB_STRING
    let encodedData;

    let startTime = Date.now();
    for (let i = 0; i < 10; i++) {
        encodedData = new TextEncoder('cseuckr').encode(inputString);
    }
    let endTime = Date.now();

    if(encodedData == null){
        throw new Error('zxing-text-encodingDemo_encode_0100 Error')
    }
   print(`zxing-text-encoding_encode_cseuckr: ${endTime - startTime} ms`)
}

// decode
function Decode_utf8_Test(){
    let encodedData = new Uint8Array(TEN_KB_BINARY)
    let decodedArray;
    
    let startTime = Date.now();
    for (let i = 0; i < 10; i++) {
        decodedArray = new TextDecoder('utf-8').decode(encodedData);
    }
    let endTime = Date.now();

    if(decodedArray == null){
        throw new Error('zxing-text-encodingDemo_decode Error')
    }
   	print(`zxing-text-encoding_decode_utf8: ${endTime - startTime} ms`)
}

function Decode_utf16_Test(){
    let encodedData = new Uint8Array(TEN_KB_BINARY)
    let decodedArray;
    
    let startTime = Date.now();
    for (let i = 0; i < 10; i++) {
        decodedArray = new TextDecoder('utf-16').decode(encodedData);
    }
    let endTime = Date.now();

    if(decodedArray == null){
        throw new Error('zxing-text-encodingDemo_decode Error')
    }
   print(`zxing-text-encoding_decode_utf16: ${endTime - startTime} ms`)
}

function Decode_gbk_Test(){
	const inputString = ONEHUNDRED_KB_STRING
    let encodedData = new TextEncoder('gbk').encode(inputString)
    let decodedArray;
    
    let startTime = Date.now();
    for (let i = 0; i < 10; i++) {
        decodedArray = new TextDecoder('gbk').decode(encodedData);
    }
    let endTime = Date.now();

    if(decodedArray == null){
        throw new Error('zxing-text-encodingDemo_decode Error')
    }
   print(`zxing-text-encoding_decode_gbk: ${endTime - startTime} ms`)
}

function Decode_csbig5_Test(){
    const inputString = ONEHUNDRED_KB_STRING
	let encodedData = new TextEncoder('csbig5').encode(inputString)
    let decodedArray;
    
    let startTime = Date.now();
    for (let i = 0; i < 10; i++) {
        decodedArray = new TextDecoder('csbig5').decode(encodedData);
    }
    let endTime = Date.now();

    if(decodedArray == null){
        throw new Error('zxing-text-encodingDemo_decode Error')
    }
   print(`zxing-text-encoding_decode_csbig5: ${endTime - startTime} ms`)
}

function Decode_eucjp_Test(){
    const inputString = ONEHUNDRED_KB_STRING;
    let encodedData = new TextEncoder('euc-jp').encode(inputString);
    let decodedArray;
    
    let startTime = Date.now();
    for (let i = 0; i < 10; i++) {
        decodedArray = new TextDecoder('euc-jp').decode(encodedData);
    }
    let endTime = Date.now();

    if(decodedArray == null){
        throw new Error('zxing-text-encodingDemo_decode Error')
    }
   print(`zxing-text-encoding_decode_eucjp: ${endTime - startTime} ms`)
}

function Decode_cseuckr_Test(){
    const inputString = ONEHUNDRED_KB_STRING
    let encodedData = new TextEncoder('cseuckr').encode(inputString)
    let decodedArray;
    
    let startTime = Date.now();
    for (let i = 0; i < 10; i++) {
        decodedArray = new TextDecoder('cseuckr').decode(encodedData);
    }
    let endTime = Date.now();

    if(decodedArray == null){
        throw new Error('zxing-text-encodingDemo_decode Error')
    }
   print(`zxing-text-encoding_decode_cseuckr: ${endTime - startTime} ms`)
}

let testNumber = 200000
function test1() {
	let arr = new Array(10)
	let len = arr.length
	let start = Date.now()
	for (let i = 0; i < testNumber; i++) {
		arr[i % len] = new TextEncoder()
	}
	let end = Date.now()
	print("zxing-text-encoding_TextEncoder: " + (end - start)  +' ms')
	for (let i = 0; i < 10; i++) {
		if (arr[i] == null) {
			print("test1 fail")
		}
	}
}

function test2() {
	let arr = new Array(10)
	let len = arr.length
	let start = Date.now()
	for (let i = 0; i < testNumber; i++) {
		arr[i % len] = new TextDecoder()
	}
	let end = Date.now()
	print("zxing-text-encoding_TextDecoder: " + (end - start) +' ms')
	for (let i = 0; i < 10; i++) {
		if (arr[i] == null) {
			print("test2 fail")
		}
	}
}

function Switch() {
    Encode_utf8_Test()
	Encode_utf16_Test()
	Encode_csbig5_Test()
	Encode_cseuckr_Test()
	Encode_csibm866_Test()
	Encode_eucjp_Test()
	Encode_gbk_Test()
    Decode_utf8_Test()
	Decode_utf16_Test()
	Decode_csbig5_Test()
	Decode_cseuckr_Test()
	Decode_eucjp_Test()
	Decode_gbk_Test()
	test1()
	test2()
}
Switch()

